const Url = require('url');
const crawler = require('./crawler');
const download = require('./download');
const { spiders } = require('../model/index');
const reader = require('./reader');

module.exports = async (entryUrl, category, maxPage = 1) => {
    const { protocol, hostname } = Url.parse(entryUrl);
    const domain = protocol + '//' + hostname;
    for (let i = 1; i <= maxPage; i++) {
        const { $ } = await crawler(entryUrl.replace('$-$', i));
        let urls = [];
        $('.case_f').each(function () {
            urls.push($(this).find('a').attr('href').trim());
        });
        urls = urls.map(el => /^https?:\/\//i.test(el) ? el : domain + el);

        for (let j = 0, len = urls.length; j < len; j++) {
            const url = urls[j];
            const detailObj = await crawler(url);

            const name = detailObj.$('h1').text().trim();
            const apkUrl = detailObj.$('.info_right').find('a').attr('href').trim();
            // const version = detailObj.html.match(/版本\s*[：:]\s*((\d+\.)+\d+)/)[1];
            const version = detailObj.html.match(/更新时间\s*[：:]\s*((\d+-)+\d+)/)[1];
            const result = await spiders.findOne({ url });
            console.log(name, version, result ? result.version : '');

            if (!result) {
                const apkPath = await download('apks', apkUrl);

                let icon = detailObj.$('[dbweburl]').attr('src');
                // icon = await download('icons', icon);

                const banners = [];
                detailObj.$('#focus').children().children().each(function () {
                    banners.push(detailObj.$(this).attr('src').trim());
                });

                await spiders.updateOne({ url }, {
                    name,
                    url,
                    version,
                    banners,
                    // icon: 'http://app.tpvstore.com/' + icon,
                    icon,
                    apkPath,
                    status: true,
                    category,
                    dlLink: apkUrl
                }, { upsert: true });
            } else if (result.version !== version) {
                const apkPath = await download('apks', apkUrl);
                await spiders.updateOne({ url, dlLink: apkUrl }, { version, apkPath, status: true });
            }
        }
    }

    console.log('抓取数据完成');
    reader(category);
};
